package com.str.set;
public class IntSet {
    
  // Each IntSet value is a set whose members are small integers.
    
  // This set is represented as follows: b[i] is true if and only if integer i 
  // is a member, and card is the cardinality.
  private final int DEFAULT_SIZE = 64;
  private final boolean[] array;
  private int count;
    
// Constructors    
  public IntSet() {
  	array = new boolean[DEFAULT_SIZE];  // All components are initially false.
    count = 0;
  }
    
  public IntSet(int m) {
  // Construct a set, initially empty, whose members will be integers in the 
  // range 0 through m-1.
    array = new boolean[m];  // All components are initially false.
    count = 0;
  }
      
// Modifiers 
  public void clear() {
  // Make this set empty.
    for (int i = 0; i < array.length; i++)
      array[i] = false;
    count = 0;
  }
    
  public void add(int i) {
  // Add i as a member of this set.
    if (i < 0 || i >= array.length)
      throw new IllegalArgumentException();   // i is out of range for this set
    if (!array[i]) {
      array[i] = true;
      count++;
    }
  }
    
  public void remove(int i) {
  // Remove x from this set.
    if (i < 0 || i >= array.length) 
      throw new IllegalArgumentException();
    if (array[i]) {
      array[i] = false;
      count--;
    }
  }
    
  public void union(IntSet that) {
  // Make this set the union of itself and that
    for (int i = 0; i < array.length; i++) {
      if (!array[i] && that.array[i]) {
        array[i] = true;
        count++;
      }
    }
  }

  public void intersection(IntSet that) {
  // Make this set the intersection of itself and that.
    for (int i = 0; i < array.length; i++)
      if (array[i] && !that.array[i]) {
        array[i] = false;
        count--;
      }
  }
    
  public void difference(IntSet that) {
  // Make this set the difference of itself and that.
    for (int i = 0; i < array.length; i++)
      if (array[i] && that.array[i]) {
        array[i] = false;
        count--;
      }
  }
    
// Accessors    
  public boolean isEmpty() {
  // Return true if and only if this set is empty.
    return count == 0;
  }

  public int size() {
  // Return the cardinality of this set.
    return count;
  }
    
  public boolean contains(int i) {
  // Return true if and only if i is a member of this set.
    if (i < 0 || i >= array.length) return false;
    return array[i];
  }
    
  public boolean equals(IntSet that) {
  // Return true if and only if this set is equal to that.
    if (count != that.count) return false;
    for (int i = 0; i < array.length; i++)
      if (array[i] != that.array[i]) return false;
    return true;
  }
    
  public boolean isSubset(IntSet that) {
  // Return true if and only if this set subsumes that.
    if (count > that.count) return false;
    for (int i = 0; i < array.length; i++)
      if (array[i] && !that.array[i]) return false;
    return true;
  }
  
// Auxiliary methods 
  public Object clone() {
    IntSet that = new IntSet(array.length);
    for (int i = 0; i < array.length; i++)
      that.array[i] = array[i];
    that.count = count;
    return that;
  }
       
  public String toString() {
  // Return a textual representation of this set, using the
  // conventional mathematical notation.
    String buf = new String("{");
    if (count > 0)
      for (int i = 0; i < array.length; i++)
        if (array[i]) buf += " " + i;
    buf += " }";
    return buf;
  }
}
